//
//  BaseNavViewController.m
//  SheMeasured
//
//  Created by ddkj on 2019/4/4.
//  Copyright © 2019 xmddkj. All rights reserved.
//

#import "BaseNavViewController.h"

@interface BaseNavViewController () <UIGestureRecognizerDelegate>

@end

@implementation BaseNavViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupDefultStyle];
    
    WEAKSELF
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
    
    self.navigationBar.barStyle = UIStatusBarStyleLightContent;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)setupDefultStyle {
    self.view.backgroundColor = [UIColor whiteColor];
    // 背景颜色
    self.navigationBar.barTintColor = [UIColor whiteColor];
    // 除标题外的其他字体颜色
    self.navigationBar.tintColor = [UIColor blackColor];
    // navigationBar是否半透明模糊处理，此项设置将影响有navigationBar的ViewController的布局
    self.navigationBar.translucent = NO;
    // 深色底白色字
    [self.navigationBar setBarStyle:UIBarStyleDefault];
    // 导航栏中间标题样式
    [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:18], NSFontAttributeName, nil]];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return [self.topViewController preferredStatusBarStyle];
}

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

- (void)setBlackBackItemForVC:(UIViewController *)vc {
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"login_icon_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick:)];
}

- (void)setWhiteBackItemForVC:(UIViewController *)vc {
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick:)];
}

- (void)leftBtnClick:(UIButton *)btn {
    if ([self.topViewController respondsToSelector:@selector(leftBtnClick)]) {
        [self.topViewController performSelector:@selector(leftBtnClick) withObject:nil afterDelay:0];
    } else {
        [super popViewControllerAnimated:YES];
    }
}

- (id)initWithRootViewController:(UIViewController *)rootViewController{
    self = [super initWithRootViewController:rootViewController];
    if(self){
        self.delegate = (id)self;
    }
    return self;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if(self.viewControllers.count < 2)
        return NO;
    else
        return YES;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if(self.viewControllers.count == 1) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:animated];
    
    if (viewController.navigationItem.leftBarButtonItem == nil && [self.viewControllers count] > 1) {
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"login_icon_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick:)];
    }
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    if(self.viewControllers.count < 2){
        return nil;
    } else {
        return [super popViewControllerAnimated:animated];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

@end
